package com.qfedu.hotelssm.controller;

import com.alibaba.excel.EasyExcel;
import com.qfedu.hotelssm.dto.CtOrderDetailDto;
import com.qfedu.hotelssm.entity.CtOrderDetail;
import com.qfedu.hotelssm.service.intf.CtOrderDetailService;
import com.qfedu.hotelssm.vo.PageBean;
import com.qfedu.hotelssm.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author dzl
 * @date 2022/8/29 14:57
 */
@RestController
@RequestMapping("/api/orderdetail/")
public class CtOrderDetailController {
    @Autowired
    private CtOrderDetailService service;

    @PostMapping("save.do")
    public R save(CtOrderDetail orderDetail) {
        return service.save(orderDetail);
    }

    @GetMapping("del.do")
    public R del(int id) {
        return service.del(id);
    }

    @GetMapping("page.do")
    public PageBean page(String userid, int page, int limit) {
        return service.page(userid, page, limit);
    }

    @PostMapping("update.do")
    public R update(CtOrderDetail orderDetail) {
        return service.update(orderDetail);
    }

    //导出
    @GetMapping("download.do")
    public void download(HttpServletResponse response) throws IOException {
        //1.设置 响应消息头 下载
        response.setHeader("content-disposition", "attachment;filename=orderdetail-" + System.currentTimeMillis() + ".xlsx");
        //2.查询菜单数据
        List<CtOrderDetailDto> list = service.all();
        //3.实现数据写出 Excel生成
        EasyExcel.write(response.getOutputStream(), CtOrderDetailDto.class).sheet(System.currentTimeMillis() + "").doWrite(list);
    }

    //导入-批量增加
    @PostMapping("upload.do")
    public R upload(MultipartFile file) throws IOException {
        return service.upload(file);
    }
    //批量删除
    @PostMapping("delids.do")
    public R delids(int[] ids){
        return service.delIds(ids);
    }
    //根据菜id进行分类统计
    @GetMapping("kindtj.do")
    public R kind(){
        return service.kindTj();
    }
}